//
//  TYCEmoticonToolBar.swift
//  TheEmoticon
//
//  Created by tangyunchuan on 2018/8/9.
//  Copyright © 2018年 tangyunchuan. All rights reserved.
//

import UIKit

@objc protocol TYCEmoticonToolBarDelegate: NSObjectProtocol {
    /// 表情工具栏选中分组项索引
    ///
    /// - Parameters:
    ///   - toolBar: 工具栏
    ///   - index: 索引
    @objc optional func emoticonToolBarDidSelectedItemIndex(toolBar: TYCEmoticonToolBar, index:Int);
}

/// 表情键盘工具栏
class TYCEmoticonToolBar: UIView {

    /// 代理
    weak var delegate: TYCEmoticonToolBarDelegate?
    /// 选中分组
    var selectedIndex: Int = 0 {
        didSet {
           //1. 取消所有的选中状态
            for btn in subviews as! [UIButton] {
                btn.isSelected = false
            }
            //2. 设置index对应的选中状态
            (subviews[selectedIndex] as! UIButton).isSelected = true
        }
    }
    
    override func awakeFromNib() {
        setUpUI()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //布局所有按钮
        let count = subviews.count
        let w = bounds.width / CGFloat(count)
        let rect = CGRect(x: 0, y: 0, width: w, height: bounds.height)
        for (i, btn) in subviews.enumerated() {
            btn.frame = rect.offsetBy(dx: CGFloat(i) * w, dy: 0)
        }
    }
    //MARK: - 监听方法
    /// 点击分组项按钮
    @objc private func clickItem(button: UIButton) {
        //通知代理执行协议方法
        delegate?.emoticonToolBarDidSelectedItemIndex!(toolBar: self, index: button.tag)
    }

}


private extension TYCEmoticonToolBar {
    func setUpUI() {
        //获取表情管理器的单例
        let manager = TYCEmoticonManager.share
        
        //从表情包的分组名称来设置按钮
        for (i,p) in manager.packages.enumerated() {
            let btn = UIButton()
            btn.setTitle(p.groupName, for: .normal)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            btn.setTitleColor(UIColor.white, for: .normal)
            btn.setTitleColor(UIColor.darkGray, for: .highlighted)
            btn.setTitleColor(UIColor.darkGray, for: .selected)

            //设置按钮的背景图片
            let imageName = "compose_emotion_table_\(p.bjImageName ?? "")_normal"
            let imageNameHL = "compose_emotion_table_\(p.bjImageName ?? "")_selected"
            
            var image = UIImage(named: imageName, in: manager.bundle, compatibleWith: nil)
            var imageHL = UIImage(named: imageNameHL, in: manager.bundle, compatibleWith: nil)
            
            let size = image?.size ?? CGSize()
            let insert = UIEdgeInsets(top: size.height * 0.5, left: size.width * 0.5, bottom: size.height * 0.5, right: size.width * 0.5)
            image = image?.resizableImage(withCapInsets: insert)
            imageHL = imageHL?.resizableImage(withCapInsets: insert)
            btn.setBackgroundImage(image, for: .normal)
            btn.setBackgroundImage(imageHL, for: .highlighted)
            btn.setBackgroundImage(imageHL, for: .selected)

            btn.sizeToFit()
            //添加按钮
            addSubview(btn)
            //设置按钮的tag
            btn.tag = i
            //添加按钮的监听方法
            btn.addTarget(self, action: #selector(clickItem), for: .touchUpInside)
        }
        //默认选中第 0  个按钮
        (subviews[0] as! UIButton).isSelected = true
    }
}
